package com.kcht.rd30.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcht.rd30.entity.TimeCalibrationEntity;
import com.kcht.rd30.mapper.ITimeCalibrationMapper;
import com.kcht.rd30.service.ITimeCalibrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class TimeCalibrationManager extends ServiceImpl<ITimeCalibrationMapper,TimeCalibrationEntity> implements ITimeCalibrationService {
    private Map<String, TimeCalibrationHandler> handlerMap=new HashMap<>();
    @Autowired
    private ITimeCalibrationMapper timeCalibrationMapper;

    /**
     * 监听ApplicationContext初始化或刷新的事件
     * 启动时执行此方法
     */
    @EventListener
    public void loadAllIP(ContextRefreshedEvent event){
        Set<String> ips = timeCalibrationMapper.findAllIp();
         if(ips==null)return;
        for (String ip : ips) {
            handlerMap.put(ip,new TimeCalibrationHandler(ip,timeCalibrationMapper));
        }
    }

    //todo 增加时注意判断是否重复ip,时间校准周期是否小于5s，小于5s则操作不正确
    //todo 更新时注意，时间校准周期是否小于5s，小于5s则操作不正确,不能更新ip
    //todo 删除时注意先closeThread
}
